//
//  SecViewController.m
//  NetworkDemo
//
//  Created by InitialC on 2017/5/22.
//  Copyright © 2017年 InitialC. All rights reserved.
//

#import "SecViewController.h"
#import <CPhotoPicker.h>
#import <CPhotoDataManager.h>
#import "AliyunManager.h"

@interface SecViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imgV1;
@property (weak, nonatomic) IBOutlet UIImageView *imgV2;
@property (weak, nonatomic) IBOutlet UIImageView *imgV3;
@property (nonatomic, strong) NSMutableArray *imageUrlArr;

@property (nonatomic, assign) int imgFlag;

@property (nonatomic, assign) int maxCount;

@end

@implementation SecViewController

- (NSMutableArray *)imageUrlArr {
    if (_imageUrlArr == nil) {
        _imageUrlArr = [NSMutableArray array];
    }
    return _imageUrlArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[AliyunManager sharedInstance] setupEnvironment];
    self.imgFlag = 0;
    self.maxCount = 3;
    [self.imageUrlArr addObject:_imgV1.image];
    [self.imageUrlArr addObject:_imgV2.image];
}
- (IBAction)updateclick:(id)sender {
    for (UIImage *image in self.imageUrlArr) {
        [[AliyunManager sharedInstance] uploadObjectAsync:UIImagePNGRepresentation(image) toFolder:@"temp_picture" withCount:self.imageUrlArr.count withProgress:^(int64_t progress) {
            NSLog(@"外面进度==%lli", progress);
        } handler:^(BOOL isSuccess, NSString * _Nullable publicURL, NSString * _Nullable fileName) {
            NSLog(@"成功==%d, 签名地址==%@, 上传的名字==%@", isSuccess, publicURL, fileName);
        }];
    }
}
- (IBAction)updatePackage:(UIButton *)sender {
    NSString *packagePath = [[NSBundle mainBundle] pathForResource:@"android1" ofType:@"apk"];
    NSData *packageData = [[NSData alloc] initWithContentsOfFile:packagePath];
    [[AliyunManager sharedInstance] setType:AliUpdateTypeOther];
    [[AliyunManager sharedInstance] uploadObjectAsync:packageData toFolder:@"android" withCount:1 withProgress:^(int64_t progress) {
        NSLog(@"外面进度==%lli", progress);
    } handler:^(BOOL isSuccess, NSString * _Nullable publicURL, NSString * _Nullable fileName) {
        NSLog(@"成功==%d, 签名地址==%@, 上传的名字==%@", isSuccess, publicURL, fileName);
    }];
}

- (IBAction)selectImg:(id)sender {
    CPhotoPicker *picker = [[CPhotoPicker alloc] init];
    __weak typeof(self) weakSelf = self;
    
    [picker showPhotoPickerWithController:self maxSelectCount:_maxCount completion:^(NSArray *imageSources, BOOL isImgType) {
        if (isImgType) {
            if (imageSources.count != 0) {
                if (weakSelf.imgFlag >= weakSelf.maxCount) {
                    weakSelf.imgFlag = 2;
                    weakSelf.imgV1.image = weakSelf.imageUrlArr[0];
                    weakSelf.imgV2.image = weakSelf.imageUrlArr[1];
                }
                weakSelf.imgFlag ++;
                switch (weakSelf.imgFlag) {
                    case 1:
                        weakSelf.imgV1.image = imageSources.firstObject;
                        break;
                    case 2:
                        weakSelf.imgV2.image = imageSources.firstObject;
                        break;
                    case 3:
                        weakSelf.imgV3.image = imageSources.firstObject;
                        break;
                    default:
                        weakSelf.imgV3.image = imageSources.firstObject;
                        break;
                }
                if (weakSelf.imageUrlArr.count >= weakSelf.maxCount) {
                    [weakSelf.imageUrlArr removeObjectAtIndex:0];
                }
                [weakSelf.imageUrlArr addObject:[AliyunGeneralObject resizeImage:imageSources.firstObject]];
            }
        }else {
            [weakSelf.imageUrlArr removeAllObjects];
            for (int i = 0; i < imageSources.count; i ++) {
                [[CPhotoDataManager shareInstance] fetchImageFromAsset:imageSources[i] type:ePhotoResolutionTypeOrigin targetSize:[UIScreen mainScreen].bounds.size result:^(UIImage *image) {
                    image = [AliyunGeneralObject resizeImage:image];
                    NSLog(@"单张图==%@", image);
                    switch (i) {
                        case 0:
                            weakSelf.imgV1.image = image;
                            break;
                        case 1:
                            weakSelf.imgV2.image = image;
                            break;
                        case 2:
                            weakSelf.imgV3.image = image;
                            break;
                        default:
                            weakSelf.imgV3.image = image;
                            break;
                    }
                    [weakSelf.imageUrlArr addObject:image];

                    
                }];
            }
        }
    }];
}

//- (NSArray *)createImagePathArr:(UIImage *)image {
//    
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
